package com.utcs.common.persistence;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.utcs.common.utils.Reflections;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;

/**
 * Created by Fly on 2017/5/23.
 */
public abstract class TreeEntity<T> extends DataEntity<T> {

    protected T parent;//父级
    protected String name;//名称
    protected String parentIds;//所有父级ID

    public TreeEntity() {
        super();
    }

    public TreeEntity(Integer id) {
        super(id);
    }
    /**
     * 父对象，只能通过子类实现，父类实现mybatis无法读取
     * @return
     */
    /**
     * jackson中的@JsonBackReference和@JsonManagedReference，以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归（infinite recursion）问题。这些标注均可用在属性或对应的get、set方法中
     * @return
     */
    @JsonBackReference
    @NotNull
    public abstract T getParent();

    /**
     * 父对象，只能通过子类实现，父类实现mybatis无法读取
     * @return
     */
    public abstract void setParent(T parent);

    @Length(max=24)
    @NotNull
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    public Integer getParentId(){
        Integer id = null;
        if (parent != null){
            id = (Integer) Reflections.getFieldValue(parent, "id");
        }
        return id!=null ? id : 0;
    }
}
